<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%	
String departmentId = request.getParameter("departmentId");
request.setAttribute("departmentId",departmentId); 
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
	body,html{
		margin: 0 auto;
		height:100%;
		width:100%;
	}
	input{
		:1px solid #9ABAE9;
		windth:150px;
		height:18px;
	}
	textarea{
		:1px solid #9ABAE9;
		windth:135px;
		height:50px;
	}
</style>
</head>
<body>

<div id="system_departmentAddMain" style="width:100%;height:100%;overflow: auto;" class="divfrist">
			<DIV id="" style="width:100%;height:0.7%;overflow:hidden;"></DIV>
			<DIV id="" style="width:100%;height:98.6%;">
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:50%;height:100%;">
							<DIV id="" class="all" style="width:100%;height:100%;max-height:100%;max-width:100%;overflow:auto;">
							   <!--内容区 -->
												<div style="padding:10px 0 10px 60px">
															    <form id="system_departmentAddff" method="post">
															        <input type="hidden" name="departmentId" id="departmentId">
															        <input type="hidden" name="departMentTID" id="departMentTID">
															        <input type="hidden" name="departMentPID" id="departMentPID">
															    	<table>
															    		<tr>
															    	 		<td>部门名称:</td>
															    			<td><input  type="text" name="departMentName" id="departMentName"></input></td>
															    		</tr>
															    		<tr>
															    			<td>上级部门:</td>
															    			<td><input type="text" name="pName" id="Pname" disabled="disabled"></textarea></td>
															    		</tr>
										
															    		<tr>
															    			<td>部门责任人:</td>
															    			<td><input type="text" name="departMent_dituy" id="departMent_dituy"></textarea></td>
															    		</tr>
															    		<tr>
															    			<td>部门描述:</td>
															    			<td><textarea    name="departMentNote" id="departMentNote"></textarea></td>
															    		</tr>							    		
															    	</table>
															    </form>
															   </div>
															    <div style="text-align:center;padding:5px">
															    	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="window.departmentAdd.adddepartment()">确认</a>
															    	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="window.departmentAdd.clearForm()">清空</a>
															    </div>	
							   <!--内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:48.8%;height:100%;">
							<DIV id="" class="allborder" style="width:100%;height:100%;max-height:100%;max-width:100%;">
								<!-- 内容区 -->
											<ul id="system_departmentAddTree" class="ztree" style="margin-top:0; width:160px;"></ul>
								<!-- 内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.37%;height:100%;overflow:hidden;"></DIV>
			</DIV>
			<DIV id="" class="div_bottom"></DIV>
	</div>
<script type="text/javascript">
(function(){
	//增加部门
	function adddepartment(){
		var zTree = $.fn.zTree.getZTreeObj("system_departmentAddTree");
		nodes = zTree.getSelectedNodes();
		if(nodes&&nodes.length>0){
			if($("#departMentTID")==$("#departMentPID").val()){
				alert("不能设置自己为自己的上级部门!请选择其他部门");
				return;
			}
		}
		$.messager.progress({
			title:'请注意',
			text:'数据正在加载.....'
		});
		$('#system_departmentAddff').form('submit', {
				url: "${pageContext.request.contextPath}/sys/sysDepartmentAction!departmentAdd.action",
				onSubmit: function(){
				var isValid = $(this).form('validate');
				if (!isValid){
					$.messager.progress('close');	// 当form不合法的时候隐藏工具条
				}
				return isValid;	// 返回false将停止form提交 
			},
			success: function(data){
				$.messager.progress('close');
				if(data=="success"){
					alert("增加部门成功");
					createUpdateTree();
				}else if(data=="same"){
					alert("已经有相同名称的部门存在");
				}else if(data=="us"){
					alert("更新成功");
				}else{
					alert("操作失败");
				}
				// 当成功提交之后隐藏进度条
			}
		});
	}
	//双击树改变部门选择
	function onClick(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("system_departmentAddTree");
		nodes = zTree.getSelectedNodes();
		$("#Pname").val(nodes[0].name);
		$("#departMentPID").val(nodes[0].id);
		
	}
	//清空
	function clearForm(){
		
	}
	window["departmentAdd"]={};
	window["departmentAdd"]["adddepartment"]=adddepartment;
	window["departmentAdd"]["clearForm"]=clearForm;
	function createUpdateTree(){
		var setting = {
				view: {
					dblClickExpand: false
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onClick: onClick
				}
			};
		$.ajax({
			   type: "POST",
			   url: "${pageContext.request.contextPath}/sys/sysDepartmentAction!getDepartmentOutSelf.action",
			   data:{departmentId:"${departmentId}"},
			   dataType:'json',
			   success: function(msg){
				   var node = new Object();
				   node.uuid=0;
				   node.id=1;
				   node.pid=0;
				   node.name="根部门";
				   node.open=true;
				   msg.push(node);
				   $.fn.zTree.init($("#system_departmentAddTree"), setting, msg);  
			   }
		});	
	}
	//加载初始化
	$(function(){	
		$.parser.parse('#system_departmentAddMain'); 
		var treeObj = $.fn.zTree.getZTreeObj("addDepartment_tree");
		var nodes = treeObj.getSelectedNodes();
		createUpdateTree();
		//如果有id表示为更新
		if("${departmentId}"!=null&&"${departmentId}"!=""){
			//加载修改数据
			$.ajax({
			   type: "POST",
			   async:false,
			   url: "${pageContext.request.contextPath}/sys/sysDepartmentAction!getDepartment.action",
			   data:{departmentId:"${departmentId}"},
			   dataType:'text',
			   success: function(msg){	
				   var temp = eval('('+msg+')');				 
				   $('#system_departmentAddff').form('load',temp);
				   var node =nodes[0].getParentNode();
				   $("#Pname").val(node.name);
				 
			   },
			   error:function(a,b,c){
				   alert("查询角色失败");
				}
			});
		}else{
			if(nodes.length>0){
				$("#Pname").val(nodes[0].name);
				$("#departMentPID").val(nodes[0].id);
			}
		}
	});
})();
</script>
</body>
</html>